<?php
/**
 * 分销模型
 */

namespace app\model;

use think\Model;

class DistributionModel extends Model
{
    // 设置表名
    protected $name = 'sh_distribution_distributor';
    
    // 设置主键
    protected $pk = 'id';
    
    // 自动写入时间戳
    protected $autoWriteTimestamp = true;
    
    /**
     * 获取分销设置
     */
    public static function getSettings()
    {
        $settings = SettingModel::where(['code' => 'distribution'])->select();
        $result = [];
        foreach ($settings as $setting) {
            $result[$setting['key']] = $setting['value'];
        }
        return $result;
    }
    
    /**
     * 保存分销设置
     */
    public static function saveSettings($data)
    {
        foreach ($data as $key => $value) {
            SettingModel::update(['value' => $value], ['code' => 'distribution', 'key' => $key]);
        }
        return true;
    }
    
    /**
     * 获取分销员列表
     */
    public static function getDistributors($filter = [], $page = 1, $limit = 15)
    {
        $query = self::alias('d')
            ->join('customer c', 'd.customer_id = c.id')
            ->field('d.*, c.name as customer_name, c.telephone, c.avatar');
            
        if (!empty($filter['name'])) {
            $query->where('c.name', 'like', '%' . $filter['name'] . '%');
        }
        
        if (!empty($filter['status'])) {
            $query->where('d.status', $filter['status']);
        }
        
        $list = $query->page($page, $limit)->select();
        $total = $query->count();
        
        return [
            'list' => $list,
            'total' => $total
        ];
    }
    
    /**
     * 获取分销员详情
     */
    public static function getDistributorInfo($id)
    {
        return self::alias('d')
            ->join('customer c', 'd.customer_id = c.id')
            ->field('d.*, c.name as customer_name, c.telephone, c.avatar')
            ->where('d.id', $id)
            ->find();
    }
    
    /**
     * 获取分销佣金记录
     */
    public static function getCommissionRecords($filter = [], $page = 1, $limit = 15)
    {
        $query = self::alias('dc')
            ->join('customer c', 'dc.customer_id = c.id')
            ->join('customer cc', 'dc.from_customer_id = cc.id', 'left')
            ->field('dc.*, c.name as customer_name, cc.name as from_customer_name');
            
        if (!empty($filter['customer_name'])) {
            $query->where('c.name', 'like', '%' . $filter['customer_name'] . '%');
        }
        
        if (!empty($filter['status'])) {
            $query->where('dc.status', $filter['status']);
        }
        
        $list = $query->page($page, $limit)->order('dc.create_time', 'desc')->select();
        $total = $query->count();
        
        return [
            'list' => $list,
            'total' => $total
        ];
    }
    
    /**
     * 添加分销员
     */
    public static function addDistributor($data)
    {
        return self::create($data);
    }
    
    /**
     * 更新分销员
     */
    public static function updateDistributor($id, $data)
    {
        return self::where(['id' => $id])->update($data);
    }
    
    /**
     * 删除分销员
     */
    public static function deleteDistributor($id)
    {
        return self::where(['id' => $id])->delete();
    }
}